package org.senseframework.support.orm.mybatis.definition;


import org.senseframework.support.orm.exception.QueryException;

import java.util.ArrayList;
import java.util.List;

/**
 * Author lanjian
 * Email  jianlan@iflytek.com
 */
public enum Where {
    EQ(" = "), GT(" > "), GE(" >= "), LT(" < "), LE(" <= "), LIKE(" LIKE "), NE(" <> "), NOTIN(" NOT IN "), IN(" IN "), BETWEEN(" BETWEEN "), NOTNULL(" IS NOT NULL "), ISNULL(" IS NULL "), NOTEMPTY(" IS NOT EMPTY "), ISEMPTY("IS EMPTY");

    /**
     * OBC专用
     *
     * @param p
     * @param value
     * @return
     */

    public final static List<Where> kv = new ArrayList<Where>();
    public final static List<Where> single = new ArrayList<Where>();
    public final static List<Where> twoV = new ArrayList<Where>();
    public final static List<Where> array = new ArrayList<Where>();

    static {
        kv.add(Where.EQ);
        kv.add(Where.GT);
        kv.add(Where.GE);
        kv.add(Where.LT);
        kv.add(Where.LE);
        kv.add(Where.LIKE);
        kv.add(Where.NE);
        array.add(Where.NOTIN);
        array.add(Where.IN);
        twoV.add(Where.BETWEEN);
        single.add(Where.NOTNULL);
        single.add(Where.NOTEMPTY);
        single.add(Where.ISEMPTY);
    }

    public Query where(String p, String pas, Object value) {
        switch (this) {
            case EQ:
                return Restrictions.eq(p, value, pas);
            case GT:
                return Restrictions.gt(p, value, pas);
            case GE:
                return Restrictions.ge(p, value, pas);
            case LT:
                return Restrictions.lt(p, value, pas);
            case LE:
                return Restrictions.le(p, value, pas);
            case NE:
                return Restrictions.ne(p, value, pas);
            case LIKE:
                return Restrictions.like(p, value, pas);
            case IN:
                return Restrictions.in(p, value, pas);
            default:
                throw new QueryException("enum-where is null.pls choose one!");
        }
    }

    public Query whereProperty(String p, String pas) {
        switch (this) {
            case NOTEMPTY:
                return Restrictions.notEmpty(p, pas);
            case ISEMPTY:
                return Restrictions.isEmpty(p, pas);
            case ISNULL:
                return Restrictions.isNull(p, pas);
            case NOTNULL:
                return Restrictions.notNull(p, pas);
            default:
                throw new QueryException("enum-where is null.pls choose one!");

        }
    }


    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    private String desc;

    Where(String desc) {
        this.desc = desc;
    }
}
